<template>
  <div>
    <h1>配送员列表</h1>
    <div>
      <el-input
        placeholder="请输入查找的配送员姓名"
        prefix-icon="el-icon-search"
        v-model="find"
        clearable
        style="width: 300px"
      >
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="search()"
        >搜索</el-button
      >
    </div>
    <el-table :data="rows" style="width: 100%" height="250">
      <el-table-column fixed prop="_id" label="ID" width="300">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="120"></el-table-column>
      <el-table-column prop="phone" label="电话" width="120"> </el-table-column>
      <el-table-column prop="address" label="地址" width="300">
      </el-table-column>
      <el-table-column prop="date" label="出生日期" width="120">
      </el-table-column>
      <el-table-column prop="busy" label="状态" width="120">
        <template slot-scope="scope">
          {{ getState(scope.row.busy) }}
        </template>
      </el-table-column>
      <el-table-column prop="storeId" label="所属商铺ID" width="300">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="operation(scope.row)">修改</el-button>
          <el-button type="text" @click="tian(scope.row)" style="color: red"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="curpage"
      :page-size="eachPage"
      layout=" prev, pager, next"
      :total="total"
    >
    </el-pagination>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确定删除么？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="shan()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="配送员" :visible.sync="dialogFormVisible">
      <el-form :model="row">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="row.name" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="row.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="住址" :label-width="formLabelWidth">
          <el-input v-model="row.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions, mapState ,mapMutations} = createNamespacedHelpers("distribution");
export default {
  computed: {
    ...mapState(["rows", "total", "curpage", "eachPage", "maxPage"]),
  },
  mounted() {
    this.get();
  },
  methods: {
    ...mapActions(["get", "update", "searchDistribution", "delete"]),
     ...mapMutations(["setCurPage"]),
    getState(state) {
      return state ? "在工" : "空闲";
    },
    operation(row) {
      this.$data.dialogFormVisible = true;
      this.$data.row = row;
    },
    submit() {
      const id = this.$data.row._id;
      const phone = this.$data.row.phone;
      const address = this.$data.row.address;
      this.update({ id, phone, address });
      this.$data.dialogFormVisible = false;
    },
    search() {
      const searchName = this.$data.find;
      this.searchDistribution({ searchName });
      this.$data.find="";
    },
    tian(row) {
      this.$data.dialogVisible = true;
      this.$data.row = row;
    },
    shan() {
      const decid = this.$data.row._id;
      this.delete({ decid });
      this.$data.dialogVisible = false;
      this.get();
    },
    handleCurrentChange(val) {
      this.setCurPage(val);
    },
  },
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: "120px",
      row: "",
      find: "",
      dialogVisible: false,
    };
  },
  watch: {
    curpage() {
      this.get();
    },
  },
};
</script>

<style></style>
